package com.wwf;

import com.wwf.dao.IUserDao;
import com.wwf.io.Resource;
import com.wwf.pojo.User;
import com.wwf.sqlsession.SqlSession;
import com.wwf.sqlsession.SqlSessionFactory;
import com.wwf.sqlsession.SqlSessionFactoryBuild;
import org.dom4j.DocumentException;
import org.junit.Test;

import java.beans.IntrospectionException;
import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

public class IPersistenceTest {
    @Test
    public  void testFindList() throws PropertyVetoException, DocumentException, IllegalAccessException, ClassNotFoundException, IntrospectionException, InstantiationException, SQLException, InvocationTargetException, NoSuchFieldException {
        InputStream in = Resource.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuild().build(in);
        SqlSession sqlSession = build.openSession();
        User user = new User();

        /*List<User> objects = c.selectList("user.findAll", user);
        for (User object : objects) {
            System.out.println(object.getId()+","+object.getUsername());
        }*/
        IUserDao userDaoImpl = sqlSession.getMapper(IUserDao.class);
        List<User> list = userDaoImpl.findAll();
        for (User user1 : list) {
            System.out.println(user1);
        }
        user.setId(5);
        user.setUsername("sdssdsd");
        User user1 = userDaoImpl.findByCondition(user);
        System.out.println(user1);

    }
    @Test
    public void testInsert() throws PropertyVetoException, DocumentException {
        InputStream in = Resource.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuild().build(in);
        SqlSession sqlSession = build.openSession();
        User user = new User();
        user.setUsername("wwf83");
        user.setPassword("123456");
        IUserDao userDaoImpl = sqlSession.getMapper(IUserDao.class);
         int effectnum = userDaoImpl.insert(user);
        System.out.println(effectnum);
    }
    @Test
    public void testUpdate() throws PropertyVetoException, DocumentException {
        InputStream in = Resource.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuild().build(in);
        SqlSession sqlSession = build.openSession();
        User user = new User();
        user.setId(5);
        user.setUsername("wwf987");
        user.setPassword("0123456");
        IUserDao userDaoImpl = sqlSession.getMapper(IUserDao.class);
        int updateNum = userDaoImpl.update(user);
        System.out.println(updateNum);
    }
    @Test
    public void testDelete() throws PropertyVetoException, DocumentException {
        InputStream in = Resource.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuild().build(in);
        SqlSession sqlSession = build.openSession();
        User user = new User();
        user.setId(6);
        IUserDao userDaoImpl = sqlSession.getMapper(IUserDao.class);
        int updateNum = userDaoImpl.delete(user);
        System.out.println(updateNum);
    }
}
